#include "st7735.h"


extern SPI_HandleTypeDef hspi1;


void LCD_WriteCommand(uint8_t cmd) {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
	//HAL_SPI_Transmit_DMA(&hspi1, &cmd, 1);
    HAL_SPI_Transmit(&hspi1, &cmd, 1, HAL_MAX_DELAY);
}

void LCD_WriteByte(uint8_t dat) {
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
	//HAL_SPI_Transmit_DMA(&hspi1, &dat, 1);
    HAL_SPI_Transmit(&hspi1, &dat, 1, HAL_MAX_DELAY);
}

void LCD_WriteHalfWord(uint16_t dat16) {
	uint16_t dat = (dat16>>8) + (dat16<<8);

	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
	//HAL_SPI_Transmit_DMA(&hspi1, (uint8_t*)&dat16, 2);
	HAL_SPI_Transmit(&hspi1, (uint8_t*)&dat, 2, HAL_MAX_DELAY);
//	HAL_SPI_Transmit(&hspi1, (uint8_t*)&dat16, 1, HAL_MAX_DELAY);
}



void Lcd_Init(void)
{
  //----ST7735S reset sequence----------------//
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  HAL_Delay(50);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  HAL_Delay(100);
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  HAL_Delay(50);

  LCD_WriteCommand(0x01); // SWRESET Software reset
  HAL_Delay(100);

  LCD_WriteCommand(0x11); //Sleep out
  HAL_Delay(120);

//  LCD_WriteCommand(0xB1);
//  LCD_WriteByte(0x01);
//  LCD_WriteByte(0x2C);
//  LCD_WriteByte(0x2D);
//
//  LCD_WriteCommand(0xB2);
//  LCD_WriteByte(0x01);
//  LCD_WriteByte(0x2C);
//  LCD_WriteByte(0x2D);
//
//  LCD_WriteCommand(0xB3);
//  LCD_WriteByte(0x01);
//  LCD_WriteByte(0x2C);
//  LCD_WriteByte(0x2D);
//  LCD_WriteByte(0x01);
//  LCD_WriteByte(0x2C);
//  LCD_WriteByte(0x2D);
//
//  LCD_WriteCommand(0xB4);
//  LCD_WriteByte(0x07);
//
//  LCD_WriteCommand(0xC0);
//  LCD_WriteByte(0xA2);
//  LCD_WriteByte(0x02);
//  LCD_WriteByte(0x84);
//
//  LCD_WriteCommand(0xC1);
//  LCD_WriteByte(0xC5);
//
//  LCD_WriteCommand(0xC2);
//  LCD_WriteByte(0x0A);
//  LCD_WriteByte(0x00);
//
//  LCD_WriteCommand(0xC3);
//  LCD_WriteByte(0x8A);
//  LCD_WriteByte(0x2A);
//
//  LCD_WriteCommand(0xC4);
//  LCD_WriteByte(0x8A);
//  LCD_WriteByte(0xEE);
//
//  LCD_WriteCommand(0xC5);
//  LCD_WriteByte(0x0E);
//
//  LCD_WriteCommand(0x36);
//  LCD_WriteByte(0xC0);
// LCD_WriteByte(0xC8);   //A8\C8
//
//  LCD_WriteCommand(0xE0);
//  LCD_WriteByte(0x0F);
//  LCD_WriteByte(0x1A);
//  LCD_WriteByte(0x0F);
//  LCD_WriteByte(0x18);
//  LCD_WriteByte(0x2F);
//  LCD_WriteByte(0x28);
//  LCD_WriteByte(0x20);
//  LCD_WriteByte(0x22);
//  LCD_WriteByte(0x1F);
//  LCD_WriteByte(0x1B);
//  LCD_WriteByte(0x23);
//  LCD_WriteByte(0x37);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x07);
//  LCD_WriteByte(0x02);
//  LCD_WriteByte(0x10);
//
//  LCD_WriteCommand(0xE1);
//  LCD_WriteByte(0x0F);
//  LCD_WriteByte(0x1B);
//  LCD_WriteByte(0x0F);
//  LCD_WriteByte(0x17);
//  LCD_WriteByte(0x33);
//  LCD_WriteByte(0x2C);
//  LCD_WriteByte(0x29);
//  LCD_WriteByte(0x2E);
//  LCD_WriteByte(0x30);
//  LCD_WriteByte(0x30);
//  LCD_WriteByte(0x39);
//  LCD_WriteByte(0x3F);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x07);
//  LCD_WriteByte(0x03);
//  LCD_WriteByte(0x10);
//
//  LCD_WriteCommand(0x2A);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x0);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x7F);
//
//  LCD_WriteCommand(0x2B);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x00);
//  LCD_WriteByte(0x9F);
//
//  LCD_WriteCommand(0xF0);
//  LCD_WriteByte(0x01);
//
//  LCD_WriteCommand(0xF6);
//  LCD_WriteByte(0x00);
//
//  LCD_WriteCommand(0x3A); //65k mode
//  LCD_WriteByte(0x05);
//  LCD_WriteCommand(0x29); //Display on

  //----ST7735S Frame Rate---------------------//
    LCD_WriteCommand(0xB1); //Frame rate 80Hz Frame rate=333k/((RTNA + 20) x (LINE + FPA + BPA))
    LCD_WriteByte(0x02);  //RTNA
    LCD_WriteByte(0x35);  //FPA
    LCD_WriteByte(0x36);   //BPA
    LCD_WriteCommand(0xB2); //Frame rate 80Hz
    LCD_WriteByte(0x02);
    LCD_WriteByte(0x35);
    LCD_WriteByte(0x36);
    LCD_WriteCommand(0xB3); //Frame rate 80Hz
    LCD_WriteByte(0x02);
    LCD_WriteByte(0x35);
    LCD_WriteByte(0x36);
    LCD_WriteByte(0x02);
    LCD_WriteByte(0x35);
    LCD_WriteByte(0x36);

    //------------------------------------Display Inversion Control-----------------------------------------//
    LCD_WriteCommand(0xB4);
    LCD_WriteByte(0x03);

    //------------------------------------ST7735S Power Sequence-----------------------------------------//
    LCD_WriteCommand(0xC0);
    LCD_WriteByte(0x62);
    LCD_WriteByte(0x02);
    LCD_WriteByte(0x04);

    LCD_WriteCommand(0xC1);
    LCD_WriteByte(0xC0);

    LCD_WriteCommand(0xC2);
    LCD_WriteByte(0x0D);
    LCD_WriteByte(0x00);

    LCD_WriteCommand(0xC3);
    LCD_WriteByte(0x8D);
    LCD_WriteByte(0x6A);

    LCD_WriteCommand(0xC4);
    LCD_WriteByte(0x8D);
    LCD_WriteByte(0x6A);
    //---------------------------------End ST7735S Power Sequence---------------------------------------//
    LCD_WriteCommand(0xC5); //VCOM
    LCD_WriteByte(0x0A);

    LCD_WriteCommand(0x36); //MX, MY, RGB mode
    LCD_WriteByte(0xA0);   //EC
//    LCD_WriteByte(0xA8);    //A8\C8
    //------------------------------------ST7735S Gamma Sequence-----------------------------------------//
    LCD_WriteCommand(0xE0);
    LCD_WriteByte(0x12);
    LCD_WriteByte(0x1C);
    LCD_WriteByte(0x10);
    LCD_WriteByte(0x18);
    LCD_WriteByte(0x33);
    LCD_WriteByte(0x2C);
    LCD_WriteByte(0x25);
    LCD_WriteByte(0x28);
    LCD_WriteByte(0x28);
    LCD_WriteByte(0x27);
    LCD_WriteByte(0x2F);
    LCD_WriteByte(0x3C);
    LCD_WriteByte(0x00);
    LCD_WriteByte(0x03);
    LCD_WriteByte(0x03);
    LCD_WriteByte(0x10);

    LCD_WriteCommand(0xE1);
    LCD_WriteByte(0x12);
    LCD_WriteByte(0x1C);
    LCD_WriteByte(0x10);
    LCD_WriteByte(0x18);
    LCD_WriteByte(0x2D);
    LCD_WriteByte(0x28);
    LCD_WriteByte(0x23);
    LCD_WriteByte(0x28);
    LCD_WriteByte(0x28);
    LCD_WriteByte(0x26);
    LCD_WriteByte(0x2F);
    LCD_WriteByte(0x3B);
    LCD_WriteByte(0x00);
    LCD_WriteByte(0x03);
    LCD_WriteByte(0x03);
    LCD_WriteByte(0x10);
    //------------------------------------End ST7735S Gamma Sequence-----------------------------------------//
    LCD_WriteCommand(0x3A); //65k mode
    LCD_WriteByte(0x05);
    LCD_WriteCommand(0x29); //Display on

    HAL_Delay(50);
}

void LCD_pushColor(uint16_t color, int cnt)
{
	uint8_t i;
	uint16_t dat = (color>>8) + (color<<8);

	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
	for (i = 1; i <= cnt; i++)
	{
	  //HAL_SPI_Transmit_DMA(&hspi1, (uint8_t*)&color, 2);
	 // HAL_SPI_Transmit(&hspi1, (uint8_t*)&color+1, 1, HAL_MAX_DELAY);
	  HAL_SPI_Transmit(&hspi1, (uint8_t*)&dat, 2, HAL_MAX_DELAY);
	}
}

void ST7735_setAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1)
{
    LCD_WriteCommand(0x2A);
	LCD_WriteHalfWord(y0);
	LCD_WriteHalfWord(y1);

    LCD_WriteCommand(0x2B);
	LCD_WriteHalfWord(x0);
	LCD_WriteHalfWord(x1);

    LCD_WriteCommand(0x2C);
}

//调用这个函数使屏幕填充颜色
 void LCD_fillScreen(uint16_t color)
{

    uint8_t j;
    ST7735_setAddrWindow(0, 0, 127, 159);

    for(j=0;j<128;j++)
		{
    		LCD_pushColor(color, 160);
		}
}

 void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t data)
 {
	uint16_t dat = (data>>8) + (data<<8);

	 ST7735_setAddrWindow(x,y,x+1,y+1);
	 HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
	 HAL_SPI_Transmit(&hspi1, (uint8_t*)&dat, 2, HAL_MAX_DELAY);
 }

 void LCD_ShowImg(const uint8_t* img)
 {
	LCD_fillScreen(WHITE); //清屏

	ST7735_setAddrWindow(10,0,113,159);		//坐标设置

	hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
	HAL_SPI_Init(&hspi1);

	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
    HAL_SPI_Transmit(&hspi1, img, 104*160, HAL_MAX_DELAY);

    hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
    HAL_SPI_Init(&hspi1);
 }
